import {
  IsEmail,
  IsMobilePhone,
  IsNotEmpty,
  IsOptional,
  IsString,
  ValidateIf,
} from 'class-validator';
import { Config } from 'src/config';
import { IsAvatarPath } from 'src/decorators/AvatarPath';
import { Type } from 'class-transformer';

export class AdminDto {
  //登录参数验证
  @IsOptional()
  @IsMobilePhone(
    'zh-CN',
    { strictMode: Config.strictMode },
    { message: '手机号码格式不正确。' },
  )
  readonly phone: string;

  @ValidateIf((v: AdminDto) => !v.phone)
  @IsString({ message: '请传递 "username" 。' })
  readonly username: string;

  @IsNotEmpty({ message: '请传递 "password" 。' })
  @IsString({ message: '字段 "password" 必须是string类型' })
  readonly password: string;

  @IsString({ message: '字段 "captcha" 必须是string类型' })
  @IsNotEmpty({ message: '请传递 "captcha" 。' })
  readonly captcha: string;

  @IsString({ message: '字段 "uuid" 必须是string类型' })
  @IsNotEmpty({ message: '请传递 "uuid" 。' })
  readonly uuid: string;
}

export class Update {
  //登录参数验证
  @IsOptional()
  @IsMobilePhone(
    'zh-CN',
    { strictMode: Config.strictMode },
    { message: '手机号码格式不正确。' },
  )
  readonly phone: string;

  @IsOptional()
  @IsString({ message: '字段 "username" 必须是string类型' })
  readonly username: string;

  @IsOptional()
  @IsString({ message: '字段 "password" 必须是string类型' })
  password: string;

  @IsOptional()
  @IsEmail({}, { message: '请传递正确的邮箱号' })
  email: string;

  @IsOptional()
  @IsAvatarPath({ message: '头像地址格式不正确' })
  avatar: string;
}

export class GetInfo {
  @IsNotEmpty({ message: '用户Id不能为空' })
  @Type(() => Number)
  readonly id: number;
}

export class Register {
  @IsNotEmpty({ message: '用户名不能为空' })
  readonly username: string;

  @IsMobilePhone(
    'zh-CN',
    { strictMode: Config.strictMode },
    { message: '手机号码格式不正确。' },
  )
  readonly phone: string;

  @IsNotEmpty({ message: '密码不能为空' })
  readonly password: string;
}
